﻿using System;
namespace Space_Conquest
{
    class Audio
    {

        // Private variables to keep track of MIDI file / repeat
        private static Microsoft.DirectX.AudioVideoPlayback.Audio musicfile;
        private static Microsoft.DirectX.AudioVideoPlayback.Audio[] audiofile = new Microsoft.DirectX.AudioVideoPlayback.Audio[20];
        private static int currentSoundIndex = 0;
        private static bool musicrepeat;

        /// <summary>
        /// Plays a WAV sound
        /// </summary>
        /// <param name="FileName"></param>
        public static void PlaySound(string FileName)
        {

            try
            {

                // Move to next audio buffer
                ++currentSoundIndex;
                if (currentSoundIndex > audiofile.Length - 1)
                    currentSoundIndex = 0;

                // Stop / dispose of audio last in this place
                if (audiofile[currentSoundIndex] != null)
                {
                    if (audiofile[currentSoundIndex].Playing)
                    {
                        audiofile[currentSoundIndex].Stop();

                    }
                    audiofile[currentSoundIndex].Dispose();
                }

                // Make and play new sound
                audiofile[currentSoundIndex] = new Microsoft.DirectX.AudioVideoPlayback.Audio(Globals.RESOURCE_DIRECTORY + FileName);
                audiofile[currentSoundIndex].Play();

            }
            catch
            {

                #if DEBUG
                Helpers.LogEvent("Space_Conquest.Audio.PlaySound() (Framework/Audio.cs)", "Could not find sound file: " + FileName);
                #endif

                Console.Beep();

            }

        }

        /// <summary>
        /// Starts playing a MID / MIDI file, only one can play at a time
        /// </summary>
        /// <param name="FileName"></param>
        /// <param name="repeat"></param>
        public static void PlayMusic(string FileName, bool repeat)
        {

            if (musicfile != null)
            {
                musicfile.Stop();
                musicfile.Dispose();
                musicfile = null;
            }

            try
            {


                musicfile = new Microsoft.DirectX.AudioVideoPlayback.Audio(Globals.RESOURCE_DIRECTORY + FileName);

                musicrepeat = repeat;
                if (repeat)
                    musicfile.Ending += new System.EventHandler(OnMusicEnding);

                musicfile.Play();

            }
            catch
            {

                #if DEBUG
                Helpers.LogEvent("Space_Conquest.Audio.PlayMusic() (Framework/Audio.cs)", "Could not find music file: " + FileName);
                #endif

                Console.Beep();

            }


        }

        /// <summary>
        /// Default is to repeat music
        /// </summary>
        /// <param name="FileName"></param>
        public static void PlayMusic(string FileName)
        {
            PlayMusic(FileName, true);
        }

        /// <summary>
        /// Stops a MID / MIDI file
        /// </summary>
        public static void StopMusic()
        {

            if (musicfile != null)
            {
                musicfile.Stop();
                musicrepeat = false;
                musicfile.Dispose();
                musicfile = null;
            }

        }

        /// <summary>
        /// Callback function for Music, used to repeat a MIDI when it ends
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void OnMusicEnding(object sender, System.EventArgs e)
        {
            musicfile.Stop();
            musicfile.Play();
        }

    }
}
